package com.faultmethon;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-03-08
 * Time：14:0
 *
 */

public class Person {

    public String name;
    public  int age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}


class Test4 {
    public static void main(String[] args) {


        Person p1= new Person("钟暖男",19);
        Person p3= new Person("钟暖男",19);

        Person p2=new Person("钟夫人",16);

        // 只有所有的成员变量相同就才会返回 true
        boolean b=p1.equals(p3);
        System.out.println(b);



    }
}



class Test5 {
    public static void main(String[] args) {

        Person p1=new Person("钟大佬",19);
        Person p2=new Person("钟大佬",19);


        // 由哈希算法
        // 只要两个对象内容相等
        // 返回的地址就相同
        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());
    }
}